﻿
 using System;
using System.Web;
using System.Reflection;
using System.Collections.Generic;
using WMS.BLL;
using System.Web.SessionState;

public class HandlerBase : IHttpHandler, IRequiresSessionState
{
    protected HttpRequest _request;
    protected HttpResponse _response;
    protected HttpContext _context;
    protected User _user;
    public string Action
    {
        get;
        set;
    }
    /// <summary>
    /// 参数集合
    /// </summary>
    public Dictionary<string, string> param = null;

    protected User GetUserInfo(string uid)
    {        
         User user = new User();
         if (_request.Cookies["User"] != null)
         {
             user.ID = CookieManager.ID;
             user.WHSSWHS = CookieManager.WHSWHS;
             user.WHSWHSName = string.IsNullOrEmpty(CookieManager.WHSWHSName) ? "" : CookieManager.WHSWHSName;
             user.PCII = CookieManager.PCII;
             user.CII = CookieManager.CII;
             user.CIIDES = CookieManager.CIIDES;             
             user.UserName = CookieManager.UserName;
         }        
        return user;
    }

    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "text/html";
        _context = context;
        this._request = context.Request;
        this._response = context.Response;
        _user = GetUserInfo(CookieManager.ID);
        try
        {
            Action = (_request["func"] ?? "").ToLower();
            Type type = this.GetType();

            MethodInfo[] methods = type.GetMethods();

            foreach (MethodInfo method in methods)
            {
                if (method.Name.Equals(Action,StringComparison.CurrentCultureIgnoreCase))
                {
                    method.Invoke(this, null);
                    return;
                }
            }
            _response.Write("");

        }
        catch (Exception e)
        {
            _response.Write("{\"error\":\"" + e + "\"}");
        }

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}
